package com.example.iqiyitest.view;

import com.example.iqiyitest.R;
import com.example.iqiyitest.base.Constants;
import com.example.iqiyitest.info.MovieDetailInfo;
import com.example.iqiyitest.utils.ImageLoader;
import com.example.iqiyitest.utils.Video;
import com.example.iqiyitest.utils.XsUtils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MovieView extends RelativeLayout implements View.OnFocusChangeListener {

	private Context mContext;
	private TextView tv_bak;
	private TextView tv_price;
	private ImageView iv_poster;
	private RelativeLayout rl_image;
	private TextView tv_copyright;
	private MovieDetailInfo movie;
	private int index;
	private int position;// -1:第一个;0:中间位置;1:最后一个
	public final static int POSITION_FIRST = -1;
	public final static int POSITION_MIDDLE = 0;
	public final static int POSITION_LAST = 1;
	public final static int POSITION_FIRST_AND_LAST = 2;
	private float[] objectAnim = new float[6];
	private ImageLoader mImageLoader;

	/**
	 * @param context
	 */
	public MovieView(Context context) {
		super(context);
		this.mContext = context;
		setId(View.generateViewId());
		mImageLoader = ImageLoader.getInstance();// zhangzhl--后进先出法 ，优先加载 最后一个
		initView();
		this.setOnFocusChangeListener(this);
	}

	public void setMovie(MovieDetailInfo movie) {
		this.movie = movie;
		if (movie == null) {
			tv_bak.setText("");
			tv_price.setText("");
			tv_price.setVisibility(View.INVISIBLE);
			 iv_poster.setImageDrawable(getResources().getDrawable(R.drawable.movie_default));
			return;
		}
		tv_bak.setText(movie.getDisplayName());
		tv_price.setVisibility(View.INVISIBLE);
		tv_price.setText("");
		ImageLoader.getInstance().loadImage(movie.getPosterLocalImg(), iv_poster, true);
		tv_copyright.setVisibility(INVISIBLE);
		tv_copyright.setText("");
	}

	private void initView() {
		 LayoutInflater.from(mContext).inflate(R.layout.iconview, this, true);
		this.setFocusable(true);
		this.setClipChildren(false);
		 tv_bak = (TextView) findViewById(R.id.tv_bak);
		 tv_price = (TextView) findViewById(R.id.tv_price);
		 iv_poster = (ImageView) findViewById(R.id.iv_poster);
		 rl_image = (RelativeLayout)findViewById(R.id.rl_image);
		 tv_copyright = (TextView)findViewById(R.id.tv_copyright);
	}

	@Override
	public void onFocusChange(View v, boolean focus) {
		MovieView view = (MovieView) v;
		tv_bak.setSelected(focus);
		tv_bak.setTextColor(
				focus ? getResources().getColor(R.color.white) : getResources().getColor(R.color.gray_light2));
		rl_image.setBackgroundColor(
				focus ? getResources().getColor(R.color.white) : getResources().getColor(R.color.transparent));
		 MovieViewAnimation.getInstance().doAnimation(view, focus);
		 if (movieFocusChangeListener != null) {
		 movieFocusChangeListener.onFocusChange(view, focus);
		 }
	}

	public float[] getObjectAnim() {
		if (0 == objectAnim[0]) {
			objectAnim = new float[2];
			objectAnim[0] = iv_poster.getScaleX();
			objectAnim[1] = iv_poster.getY();
		}
		return objectAnim;
	}

	public void setObjectAnim(float[] objectAnim) {
		this.objectAnim = objectAnim;
		// iv_poster.setScaleX(objectAnim[0]);
		// iv_poster.setScaleY(objectAnim[0]);
		// iv_poster.setTranslationY(objectAnim[1]);

		rl_image.setScaleX(objectAnim[0]);
		rl_image.setScaleY(objectAnim[0]);
		rl_image.setTranslationY(objectAnim[1]);
	}

	private MovieFocusChangeListener movieFocusChangeListener;

	public void setMovieFocusChangeListener(MovieFocusChangeListener movieFocusChangeListener) {
		this.movieFocusChangeListener = movieFocusChangeListener;
	}

	public interface MovieFocusChangeListener {
		public void onFocusChange(MovieView v, boolean focus);
	}

	public MovieDetailInfo getMovie() {
		return movie;
	}

	public void setPosition(int position) {
		this.position = position;
	}

	public int getPosition() {
		return position;
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public TextView getTv_bak() {
		return tv_bak;
	}

	public ImageView getIv_poster() {
		return iv_poster;
	}

}